package baliOnWeb.jettyMgr;

import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.net.InetAddress;  
import java.net.ServerSocket;  
import java.net.Socket;  
import org.mortbay.jetty.Connector;  
import org.mortbay.jetty.Server;  
import org.mortbay.jetty.bio.SocketConnector;  
import org.mortbay.jetty.webapp.WebAppContext;  

public class JettyStarter
{
	private static Server server;  
	
	public static void main(String[] args) throws Exception
	{
		server = new Server();  
		SocketConnector connector = new SocketConnector();  
		connector.setPort(8080);
		server.setConnectors(new Connector[] { connector });  
		
		WebAppContext context = new WebAppContext();  
		context.setServer(server);  
		context.setContextPath("/");  
		context.setWar("./WebContent");  
		server.addHandler(context);  
		
		Thread monitor = new MonitorThread();  
		monitor.start();  
		server.start();
		server.join();  
	}
		
	private static class MonitorThread extends Thread {     		
		private ServerSocket socket;     
		public MonitorThread() {  
			setDaemon(true);  
			setName("StopMonitor");        
			try {  
				socket = new ServerSocket(8079, 1, InetAddress.getByName("127.0.0.1"));  
			} catch(Exception e) {  
				throw new RuntimeException(e);  
			}  
		}  
	
		@Override  
		public void run() {  	
			Socket accept;  
			try {  
				accept = socket.accept();  
				server.stop();  // Stop Jetty server here
				accept.close();  
				socket.close();  
			} catch(Exception e) {
				throw new RuntimeException(e);  
			}  
		}  
	}
}
